package com.open.free.generator.project;

import com.open.free.generator.constant.TemplateConstants;
import com.open.free.generator.utils.FileIOUtils;
import com.open.free.generator.utils.FreemarkerUtils;
import com.open.free.generator.vo.InitProjectVo;
import freemarker.template.TemplateException;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Faye.Wang
 * @version 1.0
 * @date 2020/12/19 10:39
 * @Description
 * it's generator pom of maven project and project name
 * 生成maven 项目主Pom工程以及项目名称
 */
@Component
public class ProjectPomInit {

    @Autowired
    private FreemarkerUtils freemarkerUtils;
    @Autowired
    private MavenWrapper mavenWrapper;
    /**
     * 根据项目名称生成项目文件夹
     * @param projectName
     * @return 返回项目路径
     */
    private  String createProject(String projectName,String targetPath) throws IOException {
        String finalProjectPath = targetPath+ File.separator+projectName;
        FileIOUtils.getFileExists(finalProjectPath);
        return finalProjectPath;
    }

    private  void createMainPom(String projectPath, InitProjectVo initProjectVo,String pomTemplate) throws IOException, TemplateException {
        Map<String,Object> dataMaps = new HashMap<>();
        dataMaps.put("initProjectVo",initProjectVo);
        String content =  freemarkerUtils.processTemplateIntoString(pomTemplate,dataMaps);
        FileIOUtils.createFile(content,projectPath,"pom.xml");
    }

    public  void createMainRunJava(String projectPath, InitProjectVo initProjectVo,String pomTemplate) throws IOException, TemplateException {
        Map<String,Object> dataMaps = new HashMap<>();
        String mainClassName = FileIOUtils.dealName(initProjectVo.getProjectName(),"-");
        mainClassName = FileIOUtils.toUpperCaseFirst(mainClassName);
        dataMaps.put("initProjectVo",initProjectVo);
        dataMaps.put("mainClassName",mainClassName);
        String content =  freemarkerUtils.processTemplateIntoString(pomTemplate,dataMaps);
        FileIOUtils.createFile(content,projectPath,mainClassName+"Application.java");
    }


    public  String initProject(InitProjectVo initProjectVo,String pomTemplate) throws IOException, TemplateException {
        String projectPath =  createProject(initProjectVo.getProjectName(),initProjectVo.getTargetPath());
        createMainPom(projectPath,initProjectVo,pomTemplate);
        return projectPath;
    }

    /**
     * 生成依赖工程，用于处理业务相关工具类
     * @param initProjectVo
     * @param mainPomPath
     * @return
     */
    public String initDependencyProject(InitProjectVo initProjectVo,String mainPomPath,String template,String projectName) throws IOException, TemplateException {
        initProjectVo.setTargetPath(mainPomPath);
        initProjectVo.setProjectName(initProjectVo.getArtifactValue()+"-"+projectName);
        return initProject(initProjectVo,template);
    }

    /**
     * 生成项目maven信息
     * @param mavenVersion
     * @param mavenWrapperVersion
     * @param targetPath
     * @throws IOException
     */
    public void createMvnConfig(String mavenVersion,String mavenWrapperVersion,String targetPath) throws IOException, TemplateException {
        // 生成路径文件夹
        String  mavenPath = this.createProject(".mvn"+File.separator+"wrapper",targetPath);
        mavenWrapper.mavenWrapperGenerator(mavenVersion,mavenWrapperVersion,mavenPath);
    }

}
